﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class statistika : Form
    {
        public statistika()
        {
            InitializeComponent();
        }

        public string[,] cfg = new string[20, 2];
        public OleDbConnection StatCon = new OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=pingdb.mdb");
        public OleDbCommand StatCmd;
        public OleDbDataAdapter StatDA;

        private void statistika_Load(object sender, EventArgs e)
        {
            int i = 0;
            XmlReader pingconfig = new XmlTextReader("config.xml");
            while (pingconfig.Read())
            {
                if (pingconfig.NodeType == System.Xml.XmlNodeType.Element)
                {
                    if (pingconfig.Name == "nm")
                    {
                        pingconfig.Read();
                        cfg[i, 0] = pingconfig.Value;
                    }
                    if (pingconfig.Name == "ip")
                    {
                        pingconfig.Read();
                        cfg[i, 1] = pingconfig.Value;
                        i++;
                    }
                }
            }
            if (cfg[0, 1] == "NONE")
            { radioButton1.Enabled = false; }; radioButton1.Text = cfg[0, 0];
            if (cfg[1, 1] == "NONE")
            { radioButton2.Enabled = false; }; radioButton2.Text = cfg[1, 0];
            if (cfg[2, 1] == "NONE")
            { radioButton3.Enabled = false; }; radioButton3.Text = cfg[2, 0];
            if (cfg[3, 1] == "NONE")
            { radioButton4.Enabled = false; }; radioButton4.Text = cfg[3, 0];
            if (cfg[4, 1] == "NONE")
            { radioButton5.Enabled = false; }; radioButton5.Text = cfg[4, 0];
            if (cfg[5, 1] == "NONE")
            { radioButton6.Enabled = false; }; radioButton6.Text = cfg[5, 0];
            if (cfg[6, 1] == "NONE")
            { radioButton7.Enabled = false; }; radioButton7.Text = cfg[6, 0];
            if (cfg[7, 1] == "NONE")
            { radioButton8.Enabled = false; }; radioButton8.Text = cfg[7, 0];
            if (cfg[8, 1] == "NONE")
            { radioButton9.Enabled = false; }; radioButton9.Text = cfg[8, 0];
            if (cfg[9, 1] == "NONE")
            { radioButton10.Enabled = false; }; radioButton10.Text = cfg[9, 0];
            if (cfg[10, 1] == "NONE")
            { radioButton11.Enabled = false; }; radioButton11.Text = cfg[10, 0];
            if (cfg[11, 1] == "NONE")
            { radioButton12.Enabled = false; }; radioButton12.Text = cfg[11, 0];
            if (cfg[12, 1] == "NONE")
            { radioButton13.Enabled = false; }; radioButton13.Text = cfg[12, 0];
            if (cfg[13, 1] == "NONE")
            { radioButton14.Enabled = false; }; radioButton14.Text = cfg[13, 0];
            if (cfg[14, 1] == "NONE")
            { radioButton15.Enabled = false; }; radioButton15.Text = cfg[14, 0];
            if (cfg[15, 1] == "NONE")
            { radioButton16.Enabled = false; }; radioButton16.Text = cfg[15, 0];
            if (cfg[16, 1] == "NONE")
            { radioButton17.Enabled = false; }; radioButton17.Text = cfg[16, 0];
            if (cfg[17, 1] == "NONE")
            { radioButton18.Enabled = false; }; radioButton18.Text = cfg[17, 0];
            if (cfg[18, 1] == "NONE")
            { radioButton19.Enabled = false; }; radioButton19.Text = cfg[18, 0];
            if (cfg[19, 1] == "NONE")
            { radioButton20.Enabled = false; }; radioButton20.Text = cfg[19, 0];
        }

        private void statgetpingdata_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(((int)UpdateStep.Value * 10));
        }

        private void statgetpingdata_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StatCon.Open();
            StatCmd = new OleDbCommand("SELECT TOP " + UpdateStep.Value + " * FROM pingdata WHERE pingserver LIKE '" + label3.Text + "' ORDER BY pingdate DESC", StatCon);
            StatDA = new OleDbDataAdapter(StatCmd);
            DataSet pingDS = new DataSet();
            StatDA.Fill(pingDS, "pingdata");
            StatDA.FillSchema(pingDS, SchemaType.Source, "pingdata");
            StatCon.Close();
            DataTable pingDT = pingDS.Tables["pingdata"];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = pingDT;
        }

        private void IPSelect(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true) { label3.Text = cfg[0, 1]; }
            if (radioButton2.Checked == true) { label3.Text = cfg[1, 1]; }
            if (radioButton3.Checked == true) { label3.Text = cfg[2, 1]; }
            if (radioButton4.Checked == true) { label3.Text = cfg[3, 1]; }
            if (radioButton5.Checked == true) { label3.Text = cfg[4, 1]; }
            if (radioButton6.Checked == true) { label3.Text = cfg[5, 1]; }
            if (radioButton7.Checked == true) { label3.Text = cfg[6, 1]; }
            if (radioButton8.Checked == true) { label3.Text = cfg[7, 1]; }
            if (radioButton9.Checked == true) { label3.Text = cfg[8, 1]; }
            if (radioButton10.Checked == true) { label3.Text = cfg[9, 1]; }
            if (radioButton11.Checked == true) { label3.Text = cfg[10, 1]; }
            if (radioButton12.Checked == true) { label3.Text = cfg[11, 1]; }
            if (radioButton13.Checked == true) { label3.Text = cfg[12, 1]; }
            if (radioButton14.Checked == true) { label3.Text = cfg[13, 1]; }
            if (radioButton15.Checked == true) { label3.Text = cfg[14, 1]; }
            if (radioButton16.Checked == true) { label3.Text = cfg[15, 1]; }
            if (radioButton17.Checked == true) { label3.Text = cfg[16, 1]; }
            if (radioButton18.Checked == true) { label3.Text = cfg[17, 1]; }
            if (radioButton19.Checked == true) { label3.Text = cfg[18, 1]; }
            if (radioButton20.Checked == true) { label3.Text = cfg[19, 1]; }
            button1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            label1.Text = "0";
            while (label1.Text == "0")
            {
                while (statgetpingdata.IsBusy == false)
                {
                    statgetpingdata.RunWorkerAsync();
                }
                Application.DoEvents();
                Thread.Sleep(333);
                button1.Enabled = true;
                label1.Text = "1";
            }
       }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            label1.Text = "1";
        }
    }
}
